// pages/m_system/index.js
//http://www.cnblogs.com/baixuemin/p/6511491.html
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    show_nav: "h_nav",
    logo:'',
    qr_code:'',
    conf_data:[],
    is_online_payment: false
   
  
  },
  switch2Change:function(){
    wx.showModal({
      title:'系统提示',
      content:'尚未开通在线支持，如有需要联系管理员',
      showCancel:false

    });
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

    var that = this;
     var pages = getCurrentPages();
                  var prevPage = pages[pages.length - 2]; 
                  prevPage.setData({
                    show_nav: 'h_nav'
                  });

    //调用应用实例的方法获取全局数据
    that.setData({
      userInfo: wx.getStorageSync("userInfo"),
    });
    wx.request({
      url: app.globalData.api_url + "index.php?ac=hapi&ts=get_setting",
      data: {},
      method: "GET",
      header: { 'content-type': 'application/json' },
      success: function (res) {
        console.log(res);
        if (res.data.is_online_payment == 1) {
          that.setData({
            is_online_payment: true
          });
        } else {
          that.setData({
            is_online_payment: false
          });
        }
        that.setData({
          conf_data: res.data

        });
      }
    })
  
  },
  show_nav: function (e) {

      var that = this;

      that.setData({

        show_nav: 'Haircut_nav'

      });

  },
  chooseImageTap : function(){
    var that = this;
    wx.showActionSheet({

      itemList:['从相册中选择','拍照'],
      itemColor:"#f7982a",
      success:function(res){
         
        if (!res.cancel) {
      　　　　if(res.tapIndex == 0){
        　　　　that.chooseWxImage('album')
      　　　　}else if(res.tapIndex == 1){
        　　　　that.chooseWxImage('camera')
      　　　　}
    　　　　}

      }

    });
  },
  chooseWxImage:function(type){
    var that = this;
    wx.chooseImage({
      count:1,
      sizeType:[type],
      success:function(res){
       
        that.setData({
          logo:res.tempFilePaths[0]
        });
      }
    });
  },
  qr_chooseImageTap:function(){
        var that = this;
    wx.showActionSheet({

      itemList:['从相册中选择','拍照'],
      itemColor:"#f7982a",
      success:function(res){
         
        if (!res.cancel) {
      　　　　if(res.tapIndex == 0){
        　　　　that.qr_chooseWxImage('album')
      　　　　}else if(res.tapIndex == 1){
        　　　　that.qr_chooseWxImage('camera')
      　　　　}
    　　　　}

      }

    });
  },
  qr_chooseWxImage:function(type){
    var that = this;
    wx.chooseImage({
      count:1,
      sizeType:[type],
      success:function(res){
       
        that.setData({
          qr_code:res.tempFilePaths[0]
        });
      }
    });
  },
  bindSaveTap:function(e){
   
    var that = this;
    var is_online_payment = 0;
    if (!e.detail.value.store_address || !e.detail.value.store_name || !e.detail.value.store_iphone || !e.detail.value.store_time) {
      wx.vibrateShort();
      wx.showToast({
        title: '不能为空',
        icon: 'loading',
        duration: 2000
      });
      return;
    }

    if (e.detail.value.is_online_payment)
    {
      is_online_payment = 1;
    }
    var formData = {
      is_online_payment: is_online_payment,
      store_address: e.detail.value.store_address,
      store_name: e.detail.value.store_name,
      store_iphone: e.detail.value.store_iphone,
      store_time: e.detail.value.store_time
    　　　　};
   
    if (this.data.logo && !this.data.qr_code){
    this.upload_file(app.globalData.api_url + "index.php?ac=hapi&ts=set_conf", this.data.logo, 'store_banner', formData,function(rs){
      var m = JSON.parse(rs);
      
      if (m.is_online_payment  == 1) {
        that.setData({
          is_online_payment: true
        });
      } else {
        that.setData({
          is_online_payment: false
        });
      }
      that.setData({
        conf_data: m 
      });
    });
    } else if (!this.data.logo && this.data.qr_code){
      this.upload_file(app.globalData.api_url + "index.php?ac=hapi&ts=set_conf", this.data.qr_code, 'qr_code', formData, function (rs) {
        var m = JSON.parse(rs);

        if (m.is_online_payment == 1) {
          that.setData({
            is_online_payment: true
          });
        } else {
          that.setData({
            is_online_payment: false
          });
        }
        that.setData({
          conf_data: m
        });
      });
    } else if (this.data.logo && this.data.qr_code){
      this.upload_file(app.globalData.api_url + "index.php?ac=hapi&ts=set_conf", this.data.logo, 'store_banner', formData, function (rs) {
        var m = JSON.parse(rs);

        if (m.is_online_payment == 1) {
          that.setData({
            is_online_payment: true
          });
        } else {
          that.setData({
            is_online_payment: false
          });
        }
        that.setData({
          conf_data: m
        });
      });

      this.upload_file(app.globalData.api_url + "index.php?ac=hapi&ts=set_conf", this.data.qr_code, 'qr_code', formData, function (rs) {
        var m = JSON.parse(rs);

        if (m.is_online_payment == 1) {
          that.setData({
            is_online_payment: true
          });
        } else {
          that.setData({
            is_online_payment: false
          });
        }
        that.setData({
          conf_data: m
        });
      });
      }else{
       wx.request({
         url: app.globalData.api_url + "index.php?ac=hapi&ts=noimg_setting",
         data: formData,
         method: "POST",
         header: { 'content-type': 'application/json' },
         success: function (rs) {
           var m = JSON.parse(rs);

           if (m.is_online_payment == 1) {
             that.setData({
               is_online_payment: true
             });
           } else {
             that.setData({
               is_online_payment: false
             });
           }
           that.setData({
             conf_data: m
           });
         }
       })
      }
    wx.showToast({
      title: '保存成功',
      icon: 'success',
      duration: 2000
    })
    that.onLoad();
   
  },
  upload_file: function (url, filePath, name, formData, success, fail){

    wx.uploadFile({
      url: app.globalData.api_url + "index.php?ac=hapi&ts=setting",
      　　　　filePath: filePath,
      　　　　name: name,
      　　　　header: {
        　　　　　　'content-type': 'nultipart/form-data'
      　　　　},
      　　　　formData: formData,  //http请求中其他额外的form data
      　　　　success: function (res) {
        　　　　　　
        　　　　　　if (res.statusCode == 200 && !res.data.result_code) {
          　　　　　　　　　typeof success == "function" && success(res.data);
        　　　　　　} else {
          　　　　　　　　typeof fail == "function" && fail(res);
        　　　　　　}
      　　　　},
      　　　　fail: function (res) {
        　　　　　
        　　　　　　typeof fail == "function" && fail(res);
      　　　　}
    　　})
  }
 
})